#!/bin/sh

#set -x

test_dir=$(dirname $0)
bin_dir=$(dirname $test_dir)
SCRIPT="$bin_dir/svnbackup.sh"

# Parent temporary directory
SVNDIR="${TMPDIR:-/tmp}/SVNDIR"

# SVN repository
REPO="$SVNDIR/repo"
REPO_URL="file://localhost$REPO"

# Working copy
WORK="$SVNDIR/work"
OUTDIR="$SVNDIR/output"


oneTimeSetUp() {
    echo -n "Setting up test repository "
    
    rm -rf "$SVNDIR"
    mkdir -p "$SVNDIR"
    
    svnadmin create "$REPO" >/dev/null 2>&1
    svn checkout "$REPO_URL" "$WORK" >/dev/null 2>&1
    echo -n "."

    pushd "$WORK" >/dev/null 2>&1
    touch changes.txt
    svn add changes.txt >/dev/null 2>&1
    svn commit -m "empty changes" changes.txt >/dev/null 2>&1
    echo -n "."
    
    for i in 1 2 3 4 5 6 7 8 9 10
    do
        echo -n "."
        echo $i > changes.txt
        svn commit -m "changes $i" changes.txt >/dev/null 2>&1
    done
    popd >/dev/null 2>&1
    
    #svn log "$REPO_URL"
    echo
}

oneTimeTearDown() {
    echo '------------------------------'
    rm -rf "$SVNDIR"
}

setUp () {
    rm -f "$SVNDIR/catch_output"
    
    # Recreate the output directories
    rm -rf "$OUTDIR"
    mkdir "$OUTDIR"
    
    OUTDIR2="${OUTDIR}2"
    rm -rf "$OUTDIR2"
    mkdir -p "$OUTDIR2"
    
    REMOTEDIR="$SVNDIR/remotedir"
    rm -rf "$REMOTEDIR"
    mkdir -p "$REMOTEDIR"
    
    pushd "$OUTDIR" >/dev/null 2>&1
    
    echo '------------------------------'
}

tearDown () {
    popd >/dev/null 2>&1
}

test_svnbackup_defaults () {
    "$SCRIPT" -v "$REPO"
    outfile="$OUTDIR/dumpfile-repo-0-11.bzip2"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertSame "11" "$(cat last_saved)"
}

test_svnbackup_file_name () {
    "$SCRIPT" -v --file-name TEST --out-dir "$OUTDIR" "$REPO"
    outfile="$OUTDIR/TEST-repo-0-11.bzip2"
    assertTrue "$outfile not created" "[ -f $outfile ]"
}

test_svnbackup_increment () {
    "$SCRIPT" -v -i 3 "$REPO"
    for base in "0-3" "4-6" "7-9" "10-11"
    do
        outfile="$OUTDIR/dumpfile-repo-$base.bzip2"
        assertTrue "$outfile not created" "[ -f $outfile  ]"
    done
}

test_svnbackup_history_file () {
    histfile="$SVNDIR/history"
    "$SCRIPT" -v --history-file "$histfile" "$REPO"
    outfile="$OUTDIR/dumpfile-repo-0-11.bzip2"
    assertTrue "$histfile not created" "[ -f $histfile  ]"
    assertSame "11" "$(cat $histfile)"
}

test_svnbackup_out_dir () {
    "$SCRIPT" -v --out-dir "$OUTDIR2" "$REPO"
    outfile="$OUTDIR2/dumpfile-repo-0-11.bzip2"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertTrue "histfile not created" "[ -f $OUTDIR2/last_saved ]"
}

test_svnbackup_compress_gzip () {
    "$SCRIPT" -v --compress gzip "$REPO"
    outfile="$OUTDIR/dumpfile-repo-0-11.gz"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
}

test_svnbackup_scp () {
    "$SCRIPT" -v --scp "localhost:$REMOTEDIR" "$REPO"
    outbase=dumpfile-repo-0-11.bzip2
    outfile="$REMOTEDIR/$outbase"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertFalse "original file not removed" "[ -f $OUTDIR/$outbase ]"
}

test_svnbackup_scp_with_outdir () {
    "$SCRIPT" -v --scp "localhost:$REMOTEDIR" --out-dir "$OUTDIR" "$REPO"
    outbase=dumpfile-repo-0-11.bzip2
    outfile="$REMOTEDIR/$outbase"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertFalse "original file not removed" "[ -f $OUTDIR/$outbase ]"
}

test_svnbackup_ssh_opts () {
    "$SCRIPT" -v --scp "localhost:$REMOTEDIR" --ssh-opts "-P 22" "$REPO"
    outbase=dumpfile-repo-0-11.bzip2
    outfile="$REMOTEDIR/$outbase"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertFalse "original file not removed" "[ -f $OUTDIR/$outbase ]"
}

test_svnbackup_scp_with_identity () {
    "$SCRIPT" -v --scp "localhost:$REMOTEDIR" --identity ~/.ssh/id_rsa "$REPO"
    outbase=dumpfile-repo-0-11.bzip2
    outfile="$REMOTEDIR/$outbase"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertFalse "original file not removed" "[ -f $OUTDIR/$outbase ]"
}

test_restore_backup () {
    newrepo="$OUTDIR/newrepo"
    svnadmin create "$newrepo"

    # backup
    "$SCRIPT" -v -i 5 "$REPO"

    # restore
    bunzip2 -c "$OUTDIR/dumpfile-repo-0-5.bzip2" | svnadmin load "$newrepo"
    bunzip2 -c "$OUTDIR/dumpfile-repo-6-10.bzip2" | svnadmin load "$newrepo"
    bunzip2 -c "$OUTDIR/dumpfile-repo-11-11.bzip2" | svnadmin load "$newrepo"
    
    # check the results
    svn checkout "file://localhost/$newrepo" "newwork" 
    assertSame "10" "$(cat newwork/changes.txt)"
}

test_svnbackup_resume () {
    # Back up what we have
    "$SCRIPT" -v "$REPO"

    # Add another revision
    pushd "$WORK"
    echo latest > changes.txt
    svn commit -m "more changes"
    popd
    
    # Back that one up
    "$SCRIPT" -v "$REPO"
    outfile="$OUTDIR/dumpfile-repo-12-12.bzip2"
    assertTrue "$outfile not created" "[ -f $outfile  ]"
    assertSame "12" "$(cat last_saved)"
}


. "$test_dir/shunit2"
